package com.longtop.framework.sys.auth.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.Constant;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.auth.model.TSysAuthData;

public class AuthDataService extends BaseService {

	public static final String BEAN_ID = "authDataService";

	/**
	 * 根据角色代码取得该角色的权限
	 * 
	 * @param roleId
	 * @return
	 */
	public List getAuthDataListByRoleId(String roleId) {

		return super.queryAllByCondition("from TSysAuthData", new String[] { "roleId" }, new String[] { QueryConditions.EQUAL }, new Object[] { roleId });
	}

	/**
	 * 批量更新数据权限
	 * 
	 * @param changeMap
	 * @param sessionObject
	 * @return
	 * @throws BaseCheckedException
	 */
	public Integer[] saveAuthData(Map changeMap, SessionObject sessionObject) throws BaseCheckedException {

		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);

		TSysAuthData TSysAuthData = new TSysAuthData();
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				TSysAuthData = (TSysAuthData) iterator.next();

				super.save(TSysAuthData);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				TSysAuthData = (TSysAuthData) iterator.next();
				super.update(TSysAuthData);
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			String sql = "delete from TSysAuthData where id in(" + idList + ")";
			deleteCount = super.batchUpdate(sql);
		}

		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}

	/**
	 * 读取 数据权限authDate表 ,并根据 角色id roleId 组织成一个 Map， 此方法供 系统启动时候的Listener调用。
	 * 
	 * @param roleId
	 *            角色id， 如果为空，则取全部数据
	 * @return 数据权限的Map
	 * @author 胡砥峰
	 */
	public Map getTSysAuthDataMap(String roleId) {

		Map TSysAuthDataMap = new HashMap();

		String strSQL = "";
		if (StringUtils.isEmpty(roleId)) {
			// 设置查询sql语句，注意order by. 这部分对系统的数据权限控制有 重大作用！详细见
			// SessionObject.getGrantFilterOrgIds()函数
			strSQL = "from TSysAuthData order by roleId asc,authType asc, isIncludeSubOrg desc";
		} else {
			strSQL = "from TSysAuthData where roleId='" + roleId + "' order by roleId asc, authType asc, isIncludeSubOrg desc";
		}

		List list = this.queryAll(strSQL);
		List subList = new ArrayList();
		TSysAuthData bean = null;
		String tmpRoleId = null;
		boolean isFirst = true;
		Iterator it = list.iterator();
		while (it.hasNext()) {
			bean = (TSysAuthData) it.next();
			if (isFirst) {
				tmpRoleId = bean.getRoleId();
				isFirst = false;
			}
			if (!tmpRoleId.equals(bean.getRoleId())) {
				TSysAuthDataMap.put(tmpRoleId, subList);
				subList = new ArrayList();
			}
			subList.add(bean);
			tmpRoleId = bean.getRoleId();
		}
		if (tmpRoleId != null && subList.size() > 0) {
			TSysAuthDataMap.put(tmpRoleId, subList);
		}

		return TSysAuthDataMap;
	}
}